﻿using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Cms.Domain;
using Cms.Web.Framework.Infrastructure;

namespace Cms.Web.Framework.Controls
{
    public class PageContent : Content
    {
        private IList<ApplicationPageContent> _applicationPageAreas;
        
        public PageContent()
        {
            Class = "cmsPageContent";
            _applicationPageAreas = new List<ApplicationPageContent>();
        }
        
        public IList<ApplicationPageContent> ApplicationPageContent
        {
            get { return _applicationPageAreas; }
            protected set { _applicationPageAreas = value; }
        }

        public override void RenderPublishedContent(RequestDetails details)
        {
            if (details.Page != null)
            {
                string html = details.Page.GetPublishedContentHtml(ID, details.Locale.ID);
                ContentParser.Parse(html, this, details.RequestedUrl);
                foreach (ApplicationPageContent apc in _applicationPageAreas)
                {
                    apc.RenderPublishedContent(details);
                }
            }
        }

        public override void RenderContent(RequestDetails details)
        {
            string html = string.Empty;
            if (details.Page != null && details.ContentTranslationVersion != null)
            {
                if (details.GetQueryStringValue("previewmode").ToLower() == "page")
                {
                    html = details.Page.GetContentHtml(ID, details.Locale.ID, details.ContentTranslationVersion.Timestamp);
                    if (html == string.Empty)
                        html = "&nbsp;";
                }
                else
                {
                    html = details.Page.GetPublishedContentHtml(ID, details.Locale.ID);
                }
            }
            else if (!details.AllowContentEditing || details.Page == null)
            {
                // This happens in the Properties tab in EditPage.aspx
                html = "Lorem ipsum...";
            }
            else if (!(details.Page is ApplicationPage))
            {
                html = "&nbsp;";
            }
            ContentParser.Parse(html, this, details.RequestedUrl);
            foreach (ApplicationPageContent apc in _applicationPageAreas)
            {
                apc.RenderContent(details);
            }
        }

        public override void RenderEditAreaLinks(RequestDetails details)
        {
            if (details.Page == null)
            {
                return;
            }
            var content = details.Page.GetContent(ID);
            if (content != null && content.DeniedAdministratorGroups.Contains(details.Administrator.Group))
            {
                return;
            }
            if (details.Page is ApplicationPage)
            {
                foreach (ApplicationPageContent apc in _applicationPageAreas)
                    apc.RenderEditAreaLinks(details);
            }
            else
            {
                var areaMenu = new EditAreaMenu();
                areaMenu.ContentType = "PageContent";
                areaMenu.AreaID = ID;
                areaMenu.MasterTemplateID = details.MasterTemplate.ID;
                areaMenu.LocaleID = details.Locale.ID;
                areaMenu.PageID = details.Page.ID;
                areaMenu.SaveReturnUrl = HttpContext.Current.Server.UrlEncode("EditPage.aspx?id=" + details.Page.ID + "&tab=content");
                areaMenu.CancelReturnUrl =
                    HttpContext.Current.Server.UrlEncode("EditPage.aspx?id=" + details.Page.ID + "&tab=content");
                areaMenu.VersionID = details.ContentTranslationVersion != null
                                         ? details.ContentTranslationVersion.ID
                                         : -1;
                Controls.Add(areaMenu);
            }
        }

        protected override void AddedControl(Control control, int index)
        {
            base.AddedControl(control, index);
            if (control is ApplicationPageContent)
            {
                _applicationPageAreas.Add((ApplicationPageContent)control);
            }
        }
    }
}